package com.hx.zhg.web.config;

import com.hx.zhg.web.filter.InvalidRequestFilter;
import com.hx.zhg.web.filter.ThreadLocalFilter;
import com.hx.zhg.web.filter.WxJsSDKConfigFilter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;

/**
 * @author zhenghegong
 */
@Configuration
public class FilterConfig {

    @Value("${server.context-path}")
    protected String contextPath;

    @Bean
    public Filter invalidRequestFilter() {
        return new InvalidRequestFilter();
    }

    @Bean
    public Filter threadLocalFilter() {
        return new ThreadLocalFilter();
    }

    @Bean
    public Filter wxJsSDKConfigFilter() {
        return new WxJsSDKConfigFilter();
    }

    @Bean
    public FilterRegistrationBean invalidRequestFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        //注入过滤器
        registration.setFilter(invalidRequestFilter());
        //拦截规则
        registration.addUrlPatterns("/*");
        //过滤器名称
        registration.setName("invalidRequestFilter");
        //过滤器顺序
        registration.setOrder(1);
        return registration;
    }

    @Bean
    public FilterRegistrationBean threadLocalFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        //注入过滤器
        registration.setFilter(threadLocalFilter());
        //拦截规则
        registration.addUrlPatterns("/*");
        //过滤器名称
        registration.setName("threadLocalFilter");
        //过滤器顺序
        registration.setOrder(2);
        return registration;
    }

    @Bean
    public FilterRegistrationBean wxJsSDKConfigFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        //注入过滤器
        registration.setFilter(wxJsSDKConfigFilter());
        //拦截规则
        registration.addUrlPatterns("/wxWeb/*");
        //过滤器名称
        registration.setName("wxJsSDKConfigFilter");
        //过滤器顺序
        registration.setOrder(3);
        return registration;
    }


}
